package com.pzr.soccer.controller;

import javax.servlet.http.HttpServletRequest;

import com.pzr.soccer.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pzr.soccer.service.LoginAndRegistryService;
import com.pzr.soccer.service.UserService;

@RestController
@RequestMapping("/user")
public class LoginRegistryController {
	@Value("${client.name:soccer}")
	private String client;

	@Autowired
	MongoOperations operations;

	@Autowired
	RedisTemplate<String ,Object> redisTemplate;

	@Autowired
	LoginAndRegistryService loginAndRegistryService;
	@Autowired
	UserService userService;

	@PostMapping("/login")
	public R<LoginResult> login(@RequestBody LoginOrRegistryForm form) {
		System.out.printf("hello,%s,handling your login", form.getUsername());
		LoginResult  lr=loginAndRegistryService.login(form);
		if(lr!=null){
			return R.ok(lr,"login successfully!") ;
		}else {
			return R.fail("login failed!");
		}

	}



	@PostMapping("/registry")
	public R<LoginResult> registry(@RequestBody LoginOrRegistryForm form) {
		// TODO Auto-generated method stub
		/**
		 * 检查有没有重复注册
		 */
		if(isExistSame("username",form.getUsername())){
			return R.fail("用户名已经存在！", RT.s(RT.Security_Tag_Registry));
		}
		if(form.getMobile()!=null&&isExistSame("mobile",form.getMobile())){
			return R.fail("手机已经注册！", RT.s(RT.Security_Tag_Registry));
		}
		if(form.getEmail()!=null&&isExistSame("email",form.getEmail())){
			return R.fail("邮箱已经注册！", RT.s(RT.Security_Tag_Registry));
		}
		if(form.getQq()!=null&&isExistSame("qq",form.getQq())){
			return R.fail("qq已经注册！", RT.s(RT.Security_Tag_Registry));
		}
		if(form.getWeChat()!=null&&isExistSame("weChat",form.getWeChat())){
			return R.fail("微信已经注册！", RT.s(RT.Security_Tag_Registry));
		}
		if(form.getAliPay()!=null&&isExistSame("aliPay",form.getAliPay())){
			return R.fail("支付宝已经注册！", RT.s(RT.Security_Tag_Registry));
		}

		boolean registry = loginAndRegistryService.registry(form);
		if(registry){
			LoginResult  lr=loginAndRegistryService.login(form);
			if(lr!=null){
				return R.ok(lr,"注册成功，重新登录成功！");
			}else {
				return R.ok("注册成功，请重新登录！");
			}
		}else {
			return  R.fail("注册失败");
		}

	}


	@GetMapping("/logout")
	public R<Object> logout(HttpServletRequest request) {
	  if(loginAndRegistryService.logout(request)){
	  	return R.ok("logout successfully!",RT.s(RT.Security_Tag_Logout));
	  }else {
	  	return R.fail("logout failed!",RT.s(RT.Security_Tag_Logout));
	  }

	}

	private boolean isExistSame(String fieldName,String info){
		User user;
		user=operations.findOne(Query.query(Criteria.where(fieldName).is(info)),User.class);
		return user != null;
	}

}
